#include "mainwindow.h"
#include "mytextedit.h"
#include "ui_mainwindow.h"
#include<QDebug>
#include<QFileDialog>
#include<QFile>
#include<QMessageBox>
#include<QFont>
#include <QFontDialog>
#include<QFileSystemModel>

#if defined(QT_PRINTSUPPORT_LIB)
#include <QtPrintSupport/qtprintsupportglobal.h>
#if QT_CONFIG(printer)
#if QT_CONFIG(printdialog)
#include<QPrintDialog>
#endif
#include <QDateTime>
#include<QPrinter>
#endif
#endif
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initTreeview();

    ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
    ui->treeView->setEditTriggers(QTreeView::NoEditTriggers);
    connect(ui->treeView,&QTreeView::doubleClicked,this,&MainWindow::doubleTreeView);
    connect(ui->treeView,&QTreeView::customContextMenuRequested,this,&MainWindow::showCustomContextMenu);
    QList<int> listint;
    listint<<1000<<4000;
    ui->splitter->setSizes(listint);

//setCentralWidget(ui->tabWidget);
#if !QT_CONFIG(printer)
    ui->print->setEnabled(false);
#endif

}

void MainWindow::initTreeview()
{
    model = new MyFileSystemModel();
    model->setRootPath("E:/");

    ui->treeView->setModel(model)   ;
    ui->treeView->setRootIndex(model->index("E:/"));
    // ui->treeView->setColumnHidden(1,true);
    // ui->treeView->setColumnHidden(2,true);
    // ui->treeView->setColumnHidden(3,true);
    ui->treeView->setHeaderHidden(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::showCustomContextMenu(const QPoint &pos)
{
    QModelIndex index = ui->treeView->indexAt(pos);
    QMenu menu(this);
    if(model->isDir(index))
    {
        QString filepath = model->filePath(index);
        if(filepath.isEmpty()) filepath = "E:/";
        menu.addAction("新建文件(New File)",[=](){
            QString filename = filepath+"/新建文件"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh-mm-ss")+".txt";
            QFile file(filename);
            if(!file.open(QIODevice::ReadWrite))
            {
                QMessageBox::question(this,"失败","c创建文件失败可能是文件权限不够或文件已存在");
            }
            file.close();

        });
        menu.addAction("新建文件夹(New Folder)",[=](){
            QDir dir(filepath);
            qDebug()<<filepath;
            QString dirname = "新建文件夹"+QDateTime::currentDateTime().toString("yyyy-MM-dd hh-mm-ss");
            if(!dir.exists(dirname))
            {
                dir.mkdir(dirname);
            }
        });
        menu.addAction("重命名(Rename Dir)",[=](){
            ui->treeView->edit(index);
        });
    }else{
        menu.addAction("重命名(Rename File)",[=](){
            ui->treeView->edit(index);
        });
        menu.addAction("删除文件(Remove File)",[=](){
            QString filename = model->fileName(index);
            QString filepath = model->filePath(index);
            if(QMessageBox::question(this,"删除文件","请确实是否删除文件: "+filename+"\n     删除文件无法恢复！",QMessageBox::Yes|QMessageBox::No))
            {
                if(!QFile::remove(filepath))
                {
                    QMessageBox::question(this,"失败","删除文件"+filename+"失败\n可能是文件权限不够或文件不存在");
                }
            }
        });

    }


    menu.exec(QCursor::pos());
}

void MainWindow::doubleTreeView(const QModelIndex &index)
{
    if(!model->isDir(index))
    {
        QString filename = model->filePath(index);
        QFile file(filename);
        if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
        {
            QMessageBox::warning(this,"警告","无法打开文件\n 错误："+file.errorString());
            return;
        }
        curFile = filename;
        setWindowTitle(this->windowTitle()+" "+filename);
        QTextStream in(&file);
        QString text = in.readAll();

        MytextEdit *edit = new MytextEdit(this);
        edit->setText(text);

        ui->tabWidget->addTab(edit,curFile);
        ui->tabWidget->setCurrentIndex(ui->tabWidget->count()-1);
        file.close();

    }
}
void MainWindow::on_new_file_triggered()
{
    MytextEdit *mytextedit = new MytextEdit(this);
    ui->tabWidget->addTab(mytextedit,"newTab.xjy");
    // curFile.clear();
    // ui->textEdit->clear();

}


void MainWindow::on_open_file_triggered()
{
    QString filename = QFileDialog::getOpenFileName(this,"打开文件","","所有文件(*.*);;cpp文件(*.h *.cpp)");
    QFile file(filename);
    if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
    {
        QMessageBox::warning(this,"警告","无法打开文件\n 错误："+file.errorString());
        return;
    }
    curFile = filename;
    setWindowTitle(this->windowTitle()+" "+filename);
    QTextStream in(&file);
    QString text = in.readAll();

    MytextEdit *edit = new MytextEdit(this);
    edit->setText(text);

    ui->tabWidget->addTab(edit,curFile);
    ui->tabWidget->setCurrentIndex(ui->tabWidget->count()-1);
    file.close();
}


void MainWindow::on_save_file_triggered()
{
    MytextEdit *edit = (MytextEdit *)ui->tabWidget->currentWidget();
    if(edit)
    {
        if(edit->savefile())
        {
            ui->tabWidget->setTabText(ui->tabWidget->currentIndex(),edit->getfilename());
        }
        //file.close();
    }
}


void MainWindow::on_save_as_triggered()
{
    MytextEdit *edit = (MytextEdit *)ui->tabWidget->currentWidget();
    if(edit)
    {
        if(edit->saveAsFile())
        {
            ui->tabWidget->setTabText(ui->tabWidget->currentIndex(),edit->getfilename());
        }
    }
}


void MainWindow::on_copy_triggered()
{
    MytextEdit *edit = (MytextEdit *)ui->tabWidget->currentWidget();
    if(edit){
        edit->setCopy();
    }

}


void MainWindow::on_underline_triggered(bool checked)
{
    MytextEdit *edit = (MytextEdit *)ui->tabWidget->currentWidget();
    if(edit){
        edit->setunderline(checked);
    }
}


void MainWindow::on_paste_triggered()
{
    MytextEdit *edit = (MytextEdit *)ui->tabWidget->currentWidget();
    if(edit){
        edit->setPaste();
    }
}


void MainWindow::on_cut_triggered()
{
    MytextEdit *edit = (MytextEdit *)ui->tabWidget->currentWidget();
    if(edit){
        edit->setCut();
    }
}


void MainWindow::on_italics_triggered(bool checked)
{
    MytextEdit *edit = (MytextEdit *)ui->tabWidget->currentWidget();
    if(edit){
        edit->setItalics(checked);
    }
}


void MainWindow::on_font_triggered()
{
    MytextEdit *edit = (MytextEdit *)ui->tabWidget->currentWidget();
    if(edit){
        edit->setFont();
    }
}


void MainWindow::on_about_triggered()
{
    QMessageBox::about(this,"这是我的NotePad!","这是我的NotePad 欢迎学习和使用！");
}


void MainWindow::on_undo_triggered()
{
    MytextEdit *edit = (MytextEdit *)ui->tabWidget->currentWidget();
    if(edit){
        edit->setUndo();
    }

}


void MainWindow::on_redo_triggered()
{
    MytextEdit *edit = (MytextEdit *)ui->tabWidget->currentWidget();
    if(edit){
        edit->setRedo();
    }
}


void MainWindow::on_exit_triggered()
{
    QCoreApplication::exit();
}


void MainWindow::on_print_triggered()
{
#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printer)
    QPrinter printDev;
#if QT_CONFIG(printdialog)
    QPrintDialog dialog(&printDev,this);
    if(dialog.exec()==QDialog::Rejected)
        return;
#endif \
        //ui->textEdit->print(&printDev);
#endif
}


void MainWindow::on_bolder_triggered(bool checked)
{
    MytextEdit *edit = (MytextEdit *)ui->tabWidget->currentWidget();
    if(edit){
        edit->setBoler(checked);
    }
}


void MainWindow::on_tabWidget_tabCloseRequested(int index)
{
    ui->tabWidget->removeTab(index);
}


